package com.online.shopping.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.online.shopping.pojo.CommonResult;
import com.online.shopping.pojo.ResultPage;
import com.online.shopping.pojo.TbBrand;
import com.online.shopping.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rui
 * @version 1.0
 * @date 2020/11/11 16:22
 */
@Api(tags = "品牌管理", description = "品牌增删改查")
@RestController
@Slf4j
@RequestMapping("/manager/brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Value("${server.port}")
    private String serverPort;

    @ApiOperation("根据id查找品牌")
    @GetMapping("/findBrandById/{id}")
    public CommonResult<TbBrand> findBrandById(@PathVariable("id")@ApiParam(value = "品牌id") Long id){
        TbBrand brand = brandService.findBrandById(id);
        log.info("*****查询结果：" + brand);
        if (brand != null){
            return new CommonResult(200, "查询成功!ServerPort:" +  serverPort,brand);
        }else {
            return new CommonResult(400, "没有对应记录！，查询id：" + id);
        }
    }

    @ApiOperation("查询所有品牌")
    @GetMapping("/findAllBrands")
    public List<TbBrand> findAllBrands(){
        List<TbBrand> brands = brandService.findAllBrands();
        return brands;
    }

    @ApiOperation("添加品牌")
    @PostMapping("/addBrand")
    public CommonResult addBrand(@RequestBody TbBrand brand){
        log.info(brand.toString());
        int i = brandService.addBrand(brand);
        if (i > 0){
            return new CommonResult(200, "保存成功！ServerPort:" + serverPort, brand);
        }else {
            return new CommonResult(400, "查询所有品牌失败！");
        }
    }

    @ApiOperation("更新品牌")
    @PostMapping("/updateBrand")
    public CommonResult updateBrand(@RequestBody TbBrand brand){
        log.info(brand.toString());
        int i = brandService.updateBrand(brand);
        if (i > 0){
            return new CommonResult(200, "修改品牌成功！ServerPort:" + serverPort);
        }else {
            return new CommonResult(400, "修改品牌失败！");
        }
    }

    @ApiOperation("根据id删除品牌")
    @GetMapping("/deleteBrand/{id}")
    public CommonResult deleteBrand(@PathVariable("id")Long id){
        int i = brandService.deleteBrand(id);
        if (i > 0){
            return new CommonResult(200, "删除成功！ServerPort:" + serverPort);
        }else {
            return new CommonResult(400, "删除品牌失败！");
        }
    }

    @ApiOperation("删除多个品牌")
    @GetMapping("/deleteManyBrands")
    public CommonResult deleteManyBrands(Long[] ids){
        try {
            brandService.deleteManyBrands(ids);
            return new CommonResult(200, "删除多个品牌成功！ServerPort:" + serverPort);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "删除品牌失败！");
        }
    }

    @ApiOperation("分页")
    @GetMapping("/findByPage")
    public ResultPage findByPage(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Page<TbBrand> page = (Page<TbBrand>) brandService.findAllBrands();

        ResultPage resultPage = new ResultPage();
        resultPage.setRows(page.getResult());
        resultPage.setTotal(page.getTotal());

        return resultPage;
    }

}
